cd "C:\DRS\"

erase T9.log

local hp "1 5 20 60 120"
foreach h of local hp {

use VAR_LARGE_DAILY.dta, clear
replace retg = . if retp == .
gen y = 0
replace y = 1 if retg == .
sort simid t
gen x = 0
by simid: replace x = 1 if retp == . & retp[_n+1] ~= .
replace x = 1 if retp ~= .

gen count = 0
replace count = 1 if x == 1 & y == 1 
replace count = count[_n-1]+1 if count[_n-1] ~= 0 & x == 1 & count < `h'
replace count = count[_n-`h'] if count > `h' & x == 1 & count < .

foreach j of varlist x1-x14{
rename `j' `j'raw
gen `j' = `j'raw if count == 1
replace `j' = `j'[_n-1] if count <= `h' & count ~=1 & count ~= 0
}

foreach j of varlist g1-g6{
rename `j' `j'raw
}
foreach j of varlist r1-r13{
rename `j' `j'raw
}

local i = 1
while `i' < 15 {
gen r`i' = .
local i = `i' + 1
}

replace r1 = g1raw 
replace r2 = g2raw if g2raw ~= .
replace r3 = g3raw if g3raw ~= .
replace r4 = g4raw if g4raw ~= .
replace r5 = g5raw if g5raw ~= .
replace r6 = g6raw if g6raw ~= .

gen gl = 1 if r1 ~=.
replace gl = 2 if r2 ~=.
replace gl = 3 if r3 ~=.
replace gl = 4 if r4 ~=.
replace gl = 5 if r5 ~=.
replace gl = 6 if r6 ~=.

replace r2 = r1raw if gl== 1
replace r3 = r2raw if gl== 1
replace r4 = r3raw if gl== 1
replace r5 = r4raw if gl== 1
replace r6 = r5raw if gl== 1
replace r7 = r6raw if gl== 1
replace r8 = r7raw if gl== 1
replace r9 = r8raw if gl== 1
replace r10 = r9raw if gl== 1
replace r11 = r10raw if gl== 1
replace r12 = r11raw if gl== 1
replace r13 = r12raw if gl== 1
replace r14 = r13raw if gl== 1

replace r3 = r1raw if gl== 2
replace r4 = r2raw if gl== 2
replace r5 = r3raw if gl== 2
replace r6 = r4raw if gl== 2
replace r7 = r5raw if gl== 2
replace r8 = r6raw if gl== 2
replace r9 = r7raw if gl== 2
replace r10 = r8raw if gl== 2
replace r11 = r9raw if gl== 2
replace r12 = r10raw if gl== 2
replace r13 = r11raw if gl== 2
replace r14 = r12raw if gl== 2

replace r4 = r1raw if gl== 3
replace r5 = r2raw if gl== 3
replace r6 = r3raw if gl== 3
replace r7 = r4raw if gl== 3
replace r8 = r5raw if gl== 3
replace r9 = r6raw if gl== 3
replace r10 = r7raw if gl== 3
replace r11 = r8raw if gl== 3
replace r12 = r9raw if gl== 3
replace r13 = r10raw if gl== 3
replace r14 = r11raw if gl== 3

replace r5 = r1raw if gl== 4
replace r6 = r2raw if gl== 4
replace r7 = r3raw if gl== 4
replace r8 = r4raw if gl== 4
replace r9 = r5raw if gl== 4
replace r10 = r6raw if gl== 4
replace r11 = r7raw if gl== 4
replace r12 = r8raw if gl== 4
replace r13 = r9raw if gl== 4
replace r14 = r10raw if gl== 4

replace r6 = r1raw if gl== 5
replace r7 = r2raw if gl== 5
replace r8 = r3raw if gl== 5
replace r9 = r4raw if gl== 5
replace r10 = r5raw if gl== 5
replace r11 = r6raw if gl== 5
replace r12 = r7raw if gl== 5
replace r13 = r8raw if gl== 5
replace r14 = r9raw if gl== 5

replace r7 = r1raw if gl== 6
replace r8 = r2raw if gl== 6
replace r9 = r3raw if gl== 6
replace r10 = r4raw if gl== 6
replace r11 = r5raw if gl== 6
replace r12 = r6raw if gl== 6
replace r13 = r7raw if gl== 6
replace r14 = r8raw if gl== 6

local i = 1
foreach j of varlist x1-x14{
gen w`i' = `j'*r`i'[_n+1]
replace w`i' = 0 if w`i'== .
local i = `i' + 1
}

gen retp_aux = w1+w2+w3+w4+w5+w6+w7+w8+w9+w10+w11+w12+w13+w14
replace retp_aux = . if retp_aux == 0 & retp == .
gen retp_new = retp_aux[_n-1] if x == 1

rename retp retp_old 
rename retp_new retp

replace retp = . if family == "GAM" & t == 3814 & simtype == "1. World Funds"
replace retp = . if family == "GAM" & t == 3990 & simtype == "2. Foreign Funds"

sort simid t
gen one = 1 if y == 0
by simid: egen obs = sum(one)
gen accglobal = 1 if x == 1
by simid: replace accglobal = accglobal[_n-1]*(1+retg) if accglobal == 1 & accglobal[_n-1]~=.
gen retaccg = accglobal^(1/obs)-1
gen accactive = 1 if x == 1
by simid: replace accactive = accactive[_n-1]*(1+retp) if accactive == 1 & accactive[_n-1]~=.
gen retacca = accactive^(1/obs)-1

gen z = 0
replace z = 1 if simid ~= simid[_n+1]
gen difac = retacca - retaccg if z == 1
gen diffac = (1+difac)^(252)-1
by simid: egen diffacc = max(diffac)

by simid: egen sdg = sd(retg)
by simid: egen sda = sd(retp)
by simid: egen mg = mean(retg)
by simid: egen mp = mean(retp)

drop if z == 0

bys simt: egen mga = mean(mg)
bys simt: egen mpa = mean(mp)
gen mgann = (1+mga)^(252)-1
gen mpann = (1+mpa)^(252)-1

label var mgann "Average Return Global Fund (p.y.)"
label var mpann "Average Return Simulated Global Fund (p.y.)"
label var diffacc "Average Difference in Returns (p.y.)"
label var sdg "Std. Dev. of Returns Global Fund (p.y.)"
label var sda "Std. Dev. of Returns Simulated Global Fund (p.y.)"

log using T9.log, append
display "Simulations Minimizing the Variance of Returns"
display "Largest No. of Funds"
display "Holding Period: `h' business days"
des mgann mpann diffacc sdg sda
tabstat mgann mpann diffacc sdg sda, stats(mean) by(simt)
tabstat diffacc, stats(count) by(simt)
log close
}


local hp "1 5 20 60 120"
foreach h of local hp {

use VAR_LONG_DAILY.dta, clear
replace retg = . if retp == .
gen y = 0
replace y = 1 if retg == .
sort simid t
gen x = 0
by simid: replace x = 1 if retp == . & retp[_n+1] ~= .
replace x = 1 if retp ~= .

gen count = 0
replace count = 1 if x == 1 & y == 1 
replace count = count[_n-1]+1 if count[_n-1] ~= 0 & x == 1 & count < `h'
replace count = count[_n-`h'] if count > `h' & x == 1 & count < .

foreach j of varlist x1-x14{
rename `j' `j'raw
gen `j' = `j'raw if count == 1
replace `j' = `j'[_n-1] if count <= `h' & count ~=1 & count ~= 0
}

foreach j of varlist g1-g6{
rename `j' `j'raw
}
foreach j of varlist r1-r13{
rename `j' `j'raw
}

local i = 1
while `i' < 15 {
gen r`i' = .
local i = `i' + 1
}

replace r1 = g1raw 
replace r2 = g2raw if g2raw ~= .
replace r3 = g3raw if g3raw ~= .
replace r4 = g4raw if g4raw ~= .
replace r5 = g5raw if g5raw ~= .
replace r6 = g6raw if g6raw ~= .

gen gl = 1 if r1 ~=.
replace gl = 2 if r2 ~=.
replace gl = 3 if r3 ~=.
replace gl = 4 if r4 ~=.
replace gl = 5 if r5 ~=.
replace gl = 6 if r6 ~=.

replace r2 = r1raw if gl== 1
replace r3 = r2raw if gl== 1
replace r4 = r3raw if gl== 1
replace r5 = r4raw if gl== 1
replace r6 = r5raw if gl== 1
replace r7 = r6raw if gl== 1
replace r8 = r7raw if gl== 1
replace r9 = r8raw if gl== 1
replace r10 = r9raw if gl== 1
replace r11 = r10raw if gl== 1
replace r12 = r11raw if gl== 1
replace r13 = r12raw if gl== 1
replace r14 = r13raw if gl== 1

replace r3 = r1raw if gl== 2
replace r4 = r2raw if gl== 2
replace r5 = r3raw if gl== 2
replace r6 = r4raw if gl== 2
replace r7 = r5raw if gl== 2
replace r8 = r6raw if gl== 2
replace r9 = r7raw if gl== 2
replace r10 = r8raw if gl== 2
replace r11 = r9raw if gl== 2
replace r12 = r10raw if gl== 2
replace r13 = r11raw if gl== 2
replace r14 = r12raw if gl== 2

replace r4 = r1raw if gl== 3
replace r5 = r2raw if gl== 3
replace r6 = r3raw if gl== 3
replace r7 = r4raw if gl== 3
replace r8 = r5raw if gl== 3
replace r9 = r6raw if gl== 3
replace r10 = r7raw if gl== 3
replace r11 = r8raw if gl== 3
replace r12 = r9raw if gl== 3
replace r13 = r10raw if gl== 3
replace r14 = r11raw if gl== 3

replace r5 = r1raw if gl== 4
replace r6 = r2raw if gl== 4
replace r7 = r3raw if gl== 4
replace r8 = r4raw if gl== 4
replace r9 = r5raw if gl== 4
replace r10 = r6raw if gl== 4
replace r11 = r7raw if gl== 4
replace r12 = r8raw if gl== 4
replace r13 = r9raw if gl== 4
replace r14 = r10raw if gl== 4

replace r6 = r1raw if gl== 5
replace r7 = r2raw if gl== 5
replace r8 = r3raw if gl== 5
replace r9 = r4raw if gl== 5
replace r10 = r5raw if gl== 5
replace r11 = r6raw if gl== 5
replace r12 = r7raw if gl== 5
replace r13 = r8raw if gl== 5
replace r14 = r9raw if gl== 5

replace r7 = r1raw if gl== 6
replace r8 = r2raw if gl== 6
replace r9 = r3raw if gl== 6
replace r10 = r4raw if gl== 6
replace r11 = r5raw if gl== 6
replace r12 = r6raw if gl== 6
replace r13 = r7raw if gl== 6
replace r14 = r8raw if gl== 6

local i = 1
foreach j of varlist x1-x14{
gen w`i' = `j'*r`i'[_n+1]
replace w`i' = 0 if w`i'== .
local i = `i' + 1
}

gen retp_aux = w1+w2+w3+w4+w5+w6+w7+w8+w9+w10+w11+w12+w13+w14
replace retp_aux = . if retp_aux == 0 & retp == .
gen retp_new = retp_aux[_n-1] if x == 1

rename retp retp_old 
rename retp_new retp

replace retp = . if family == "GAM" & t == 3814 & simtype == "1. World Funds"
replace retp = . if family == "GAM" & t == 3990 & simtype == "2. Foreign Funds"

sort simid t
gen one = 1 if y == 0
by simid: egen obs = sum(one)
gen accglobal = 1 if x == 1
by simid: replace accglobal = accglobal[_n-1]*(1+retg) if accglobal == 1 & accglobal[_n-1]~=.
gen retaccg = accglobal^(1/obs)-1
gen accactive = 1 if x == 1
by simid: replace accactive = accactive[_n-1]*(1+retp) if accactive == 1 & accactive[_n-1]~=.
gen retacca = accactive^(1/obs)-1

gen z = 0
replace z = 1 if simid ~= simid[_n+1]
gen difac = retacca - retaccg if z == 1
gen diffac = (1+difac)^(252)-1
by simid: egen diffacc = max(diffac)

by simid: egen sdg = sd(retg)
by simid: egen sda = sd(retp)
by simid: egen mg = mean(retg)
by simid: egen mp = mean(retp)

drop if z == 0

bys simt: egen mga = mean(mg)
bys simt: egen mpa = mean(mp)
gen mgann = (1+mga)^(252)-1
gen mpann = (1+mpa)^(252)-1

label var mgann "Average Return Global Fund (p.y.)"
label var mpann "Average Return Simulated Global Fund (p.y.)"
label var diffacc "Average Difference in Returns (p.y.)"
label var sdg "Std. Dev. of Returns Global Fund (p.y.)"
label var sda "Std. Dev. of Returns Simulated Global Fund (p.y.)"

log using T9.log, append
display "Simulations Minimizing the Variance of Returns"
display "Longest Available Sample"
display "Holding Period: `h' business days"
des mgann mpann diffacc sdg sda
tabstat mgann mpann diffacc sdg sda, stats(mean) by(simt)
tabstat diffacc, stats(count) by(simt)
log close
}


local hp "1 5 20 60 120"
foreach h of local hp {

use EXP_LARGE_DAILY.dta, clear
replace retg = . if retp == .
gen y = 0
replace y = 1 if retg == .
sort simid t
gen x = 0
by simid: replace x = 1 if retp == . & retp[_n+1] ~= .
replace x = 1 if retp ~= .

gen count = 0
replace count = 1 if x == 1 & y == 1 
replace count = count[_n-1]+1 if count[_n-1] ~= 0 & x == 1 & count < `h'
replace count = count[_n-`h'] if count > `h' & x == 1 & count < .

foreach j of varlist x1-x14{
rename `j' `j'raw
gen `j' = `j'raw if count == 1
replace `j' = `j'[_n-1] if count <= `h' & count ~=1 & count ~= 0
}

foreach j of varlist g1-g6{
rename `j' `j'raw
}
foreach j of varlist r1-r13{
rename `j' `j'raw
}

local i = 1
while `i' < 15 {
gen r`i' = .
local i = `i' + 1
}

replace r1 = g1raw 
replace r2 = g2raw if g2raw ~= .
replace r3 = g3raw if g3raw ~= .
replace r4 = g4raw if g4raw ~= .
replace r5 = g5raw if g5raw ~= .
replace r6 = g6raw if g6raw ~= .

gen gl = 1 if r1 ~=.
replace gl = 2 if r2 ~=.
replace gl = 3 if r3 ~=.
replace gl = 4 if r4 ~=.
replace gl = 5 if r5 ~=.
replace gl = 6 if r6 ~=.

replace r2 = r1raw if gl== 1
replace r3 = r2raw if gl== 1
replace r4 = r3raw if gl== 1
replace r5 = r4raw if gl== 1
replace r6 = r5raw if gl== 1
replace r7 = r6raw if gl== 1
replace r8 = r7raw if gl== 1
replace r9 = r8raw if gl== 1
replace r10 = r9raw if gl== 1
replace r11 = r10raw if gl== 1
replace r12 = r11raw if gl== 1
replace r13 = r12raw if gl== 1
replace r14 = r13raw if gl== 1

replace r3 = r1raw if gl== 2
replace r4 = r2raw if gl== 2
replace r5 = r3raw if gl== 2
replace r6 = r4raw if gl== 2
replace r7 = r5raw if gl== 2
replace r8 = r6raw if gl== 2
replace r9 = r7raw if gl== 2
replace r10 = r8raw if gl== 2
replace r11 = r9raw if gl== 2
replace r12 = r10raw if gl== 2
replace r13 = r11raw if gl== 2
replace r14 = r12raw if gl== 2

replace r4 = r1raw if gl== 3
replace r5 = r2raw if gl== 3
replace r6 = r3raw if gl== 3
replace r7 = r4raw if gl== 3
replace r8 = r5raw if gl== 3
replace r9 = r6raw if gl== 3
replace r10 = r7raw if gl== 3
replace r11 = r8raw if gl== 3
replace r12 = r9raw if gl== 3
replace r13 = r10raw if gl== 3
replace r14 = r11raw if gl== 3

replace r5 = r1raw if gl== 4
replace r6 = r2raw if gl== 4
replace r7 = r3raw if gl== 4
replace r8 = r4raw if gl== 4
replace r9 = r5raw if gl== 4
replace r10 = r6raw if gl== 4
replace r11 = r7raw if gl== 4
replace r12 = r8raw if gl== 4
replace r13 = r9raw if gl== 4
replace r14 = r10raw if gl== 4

replace r6 = r1raw if gl== 5
replace r7 = r2raw if gl== 5
replace r8 = r3raw if gl== 5
replace r9 = r4raw if gl== 5
replace r10 = r5raw if gl== 5
replace r11 = r6raw if gl== 5
replace r12 = r7raw if gl== 5
replace r13 = r8raw if gl== 5
replace r14 = r9raw if gl== 5

replace r7 = r1raw if gl== 6
replace r8 = r2raw if gl== 6
replace r9 = r3raw if gl== 6
replace r10 = r4raw if gl== 6
replace r11 = r5raw if gl== 6
replace r12 = r6raw if gl== 6
replace r13 = r7raw if gl== 6
replace r14 = r8raw if gl== 6

local i = 1
foreach j of varlist x1-x14{
gen w`i' = `j'*r`i'[_n+1]
replace w`i' = 0 if w`i'== .
local i = `i' + 1
}

gen retp_aux = w1+w2+w3+w4+w5+w6+w7+w8+w9+w10+w11+w12+w13+w14
replace retp_aux = . if retp_aux == 0 & retp == .
gen retp_new = retp_aux[_n-1] if x == 1

rename retp retp_old 
rename retp_new retp

replace retp = . if family == "GAM" & t == 3814 & simtype == "1. World Funds"
replace retp = . if family == "GAM" & t == 3990 & simtype == "2. Foreign Funds"

sort simid t
gen one = 1 if y == 0
by simid: egen obs = sum(one)
gen accglobal = 1 if x == 1
by simid: replace accglobal = accglobal[_n-1]*(1+retg) if accglobal == 1 & accglobal[_n-1]~=.
gen retaccg = accglobal^(1/obs)-1
gen accactive = 1 if x == 1
by simid: replace accactive = accactive[_n-1]*(1+retp) if accactive == 1 & accactive[_n-1]~=.
gen retacca = accactive^(1/obs)-1

gen z = 0
replace z = 1 if simid ~= simid[_n+1]
gen difac = retacca - retaccg if z == 1
gen diffac = (1+difac)^(252)-1
by simid: egen diffacc = max(diffac)

by simid: egen sdg = sd(retg)
by simid: egen sda = sd(retp)
by simid: egen mg = mean(retg)
by simid: egen mp = mean(retp)

drop if z == 0

bys simt: egen mga = mean(mg)
bys simt: egen mpa = mean(mp)
gen mgann = (1+mga)^(252)-1
gen mpann = (1+mpa)^(252)-1

label var mgann "Average Return Global Fund (p.y.)"
label var mpann "Average Return Simulated Global Fund (p.y.)"
label var diffacc "Average Difference in Returns (p.y.)"
label var sdg "Std. Dev. of Returns Global Fund (p.y.)"
label var sda "Std. Dev. of Returns Simulated Global Fund (p.y.)"

log using T9.log, append
display "Simulations Maximizing Expected Returns"
display "Largest No. of Funds"
display "Holding Period: `h' business days"
des mgann mpann diffacc sdg sda
tabstat mgann mpann diffacc sdg sda, stats(mean) by(simt)
tabstat diffacc, stats(count) by(simt)
log close
}


local hp "1 5 20 60 120"
foreach h of local hp {

use EXP_LONG_DAILY.dta, clear
replace retg = . if retp == .
gen y = 0
replace y = 1 if retg == .
sort simid t
gen x = 0
by simid: replace x = 1 if retp == . & retp[_n+1] ~= .
replace x = 1 if retp ~= .

gen count = 0
replace count = 1 if x == 1 & y == 1 
replace count = count[_n-1]+1 if count[_n-1] ~= 0 & x == 1 & count < `h'
replace count = count[_n-`h'] if count > `h' & x == 1 & count < .

foreach j of varlist x1-x14{
rename `j' `j'raw
gen `j' = `j'raw if count == 1
replace `j' = `j'[_n-1] if count <= `h' & count ~=1 & count ~= 0
}

foreach j of varlist g1-g6{
rename `j' `j'raw
}
foreach j of varlist r1-r13{
rename `j' `j'raw
}

local i = 1
while `i' < 15 {
gen r`i' = .
local i = `i' + 1
}

replace r1 = g1raw 
replace r2 = g2raw if g2raw ~= .
replace r3 = g3raw if g3raw ~= .
replace r4 = g4raw if g4raw ~= .
replace r5 = g5raw if g5raw ~= .
replace r6 = g6raw if g6raw ~= .

gen gl = 1 if r1 ~=.
replace gl = 2 if r2 ~=.
replace gl = 3 if r3 ~=.
replace gl = 4 if r4 ~=.
replace gl = 5 if r5 ~=.
replace gl = 6 if r6 ~=.

replace r2 = r1raw if gl== 1
replace r3 = r2raw if gl== 1
replace r4 = r3raw if gl== 1
replace r5 = r4raw if gl== 1
replace r6 = r5raw if gl== 1
replace r7 = r6raw if gl== 1
replace r8 = r7raw if gl== 1
replace r9 = r8raw if gl== 1
replace r10 = r9raw if gl== 1
replace r11 = r10raw if gl== 1
replace r12 = r11raw if gl== 1
replace r13 = r12raw if gl== 1
replace r14 = r13raw if gl== 1

replace r3 = r1raw if gl== 2
replace r4 = r2raw if gl== 2
replace r5 = r3raw if gl== 2
replace r6 = r4raw if gl== 2
replace r7 = r5raw if gl== 2
replace r8 = r6raw if gl== 2
replace r9 = r7raw if gl== 2
replace r10 = r8raw if gl== 2
replace r11 = r9raw if gl== 2
replace r12 = r10raw if gl== 2
replace r13 = r11raw if gl== 2
replace r14 = r12raw if gl== 2

replace r4 = r1raw if gl== 3
replace r5 = r2raw if gl== 3
replace r6 = r3raw if gl== 3
replace r7 = r4raw if gl== 3
replace r8 = r5raw if gl== 3
replace r9 = r6raw if gl== 3
replace r10 = r7raw if gl== 3
replace r11 = r8raw if gl== 3
replace r12 = r9raw if gl== 3
replace r13 = r10raw if gl== 3
replace r14 = r11raw if gl== 3

replace r5 = r1raw if gl== 4
replace r6 = r2raw if gl== 4
replace r7 = r3raw if gl== 4
replace r8 = r4raw if gl== 4
replace r9 = r5raw if gl== 4
replace r10 = r6raw if gl== 4
replace r11 = r7raw if gl== 4
replace r12 = r8raw if gl== 4
replace r13 = r9raw if gl== 4
replace r14 = r10raw if gl== 4

replace r6 = r1raw if gl== 5
replace r7 = r2raw if gl== 5
replace r8 = r3raw if gl== 5
replace r9 = r4raw if gl== 5
replace r10 = r5raw if gl== 5
replace r11 = r6raw if gl== 5
replace r12 = r7raw if gl== 5
replace r13 = r8raw if gl== 5
replace r14 = r9raw if gl== 5

replace r7 = r1raw if gl== 6
replace r8 = r2raw if gl== 6
replace r9 = r3raw if gl== 6
replace r10 = r4raw if gl== 6
replace r11 = r5raw if gl== 6
replace r12 = r6raw if gl== 6
replace r13 = r7raw if gl== 6
replace r14 = r8raw if gl== 6

local i = 1
foreach j of varlist x1-x14{
gen w`i' = `j'*r`i'[_n+1]
replace w`i' = 0 if w`i'== .
local i = `i' + 1
}

gen retp_aux = w1+w2+w3+w4+w5+w6+w7+w8+w9+w10+w11+w12+w13+w14
replace retp_aux = . if retp_aux == 0 & retp == .
gen retp_new = retp_aux[_n-1] if x == 1

rename retp retp_old 
rename retp_new retp

replace retp = . if family == "GAM" & t == 3814 & simtype == "1. World Funds"
replace retp = . if family == "GAM" & t == 3990 & simtype == "2. Foreign Funds"

sort simid t
gen one = 1 if y == 0
by simid: egen obs = sum(one)
gen accglobal = 1 if x == 1
by simid: replace accglobal = accglobal[_n-1]*(1+retg) if accglobal == 1 & accglobal[_n-1]~=.
gen retaccg = accglobal^(1/obs)-1
gen accactive = 1 if x == 1
by simid: replace accactive = accactive[_n-1]*(1+retp) if accactive == 1 & accactive[_n-1]~=.
gen retacca = accactive^(1/obs)-1

gen z = 0
replace z = 1 if simid ~= simid[_n+1]
gen difac = retacca - retaccg if z == 1
gen diffac = (1+difac)^(252)-1
by simid: egen diffacc = max(diffac)

by simid: egen sdg = sd(retg)
by simid: egen sda = sd(retp)
by simid: egen mg = mean(retg)
by simid: egen mp = mean(retp)

drop if z == 0

bys simt: egen mga = mean(mg)
bys simt: egen mpa = mean(mp)
gen mgann = (1+mga)^(252)-1
gen mpann = (1+mpa)^(252)-1

label var mgann "Average Return Global Fund (p.y.)"
label var mpann "Average Return Simulated Global Fund (p.y.)"
label var diffacc "Average Difference in Returns (p.y.)"
label var sdg "Std. Dev. of Returns Global Fund (p.y.)"
label var sda "Std. Dev. of Returns Simulated Global Fund (p.y.)"

log using T9.log, append
display "Simulations Maximizing Expected Returns"
display "Longest Available Sample"
display "Holding Period: `h' business days"
des mgann mpann diffacc sdg sda
tabstat mgann mpann diffacc sdg sda, stats(mean) by(simt)
tabstat diffacc, stats(count) by(simt)
log close
}


